/*
* Copyright 2011 Katsunori Koyanagi
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/

package sweezy.core.log.writers
{
	import flash.utils.getDefinitionByName;
	import sweezy.core.log.LogLevel;
	import sweezy.core.log.LogWriter;

	public class MonsterDebuggerLogWriter extends LogWriter
	{

		public static var colorDebug:uint;

		public static var colorError:uint;

		public static var colorFatal:uint;

		public static var colorInfo:uint;

		public static var colorWarn:uint;

		private static var _enabled:Boolean;

		private static var _functions:Vector.<String>;

		private static var _trace:Function;

		private static var _version3:Boolean;

		private static function init():void
		{
			var monsterDebugger:Class;

			try
			{
				monsterDebugger = getDefinitionByName("com.demonsters.debugger.MonsterDebugger") as Class;
				_trace = monsterDebugger["trace"];
				_version3 = true;
			}
			catch (e:Error)
			{
			}

			if (monsterDebugger === null)
			{
				try
				{
					monsterDebugger = getDefinitionByName("nl.demonsters.debugger.MonsterDebugger") as Class;
					_trace = monsterDebugger["trace"];
					_version3 = false;
				}
				catch (e:Error)
				{
				}
			}

			_enabled = _trace !== null;
			colorDebug = 0x111111;
			colorWarn = 0xff0000;
			colorError = 0xff3300;
			colorInfo = 0x0000ff;
			colorFatal = 0xff3300;
		}

		{
			init();
		}

		public function MonsterDebuggerLogWriter()
		{
		}

		override protected function writeString(level:uint, name:String, logString:String):void
		{
			if (!_enabled)
			{
				return;
			}

			var color:uint = 0x000000;
			switch (level)
			{
				case LogLevel.DEBUG:
					color = colorDebug;
					break;
				case LogLevel.INFO:
					color = colorInfo;
					break;
				case LogLevel.WARN:
					color = colorWarn;
					break;
				case LogLevel.ERROR:
					color = colorError;
					break;
				case LogLevel.FATAL:
					color = colorFatal;
					break;
			}

			if (_version3)
			{
				_trace(name, logString, null, null, color);
			}
			else
			{
				_trace(name, logString, color);
			}
		}
	}
}
